import os
import socket
import RPi.GPIO as GPIO

host = '0.0.0.0'
port = 2014
pin_id = 3

def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind((host,port))
    sock.listen(0)
    print 'begin listen..'

    request, addr = sock.accept()
    #print addr
    print addr[0]+'connected, gpio start!'
    
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(pin_id,GPIO.OUT)
    GPIO.output(pin_id,True)

    while True:
        data = request.recv(8)
        print 'recieved:'+data

        if data=='bye':
            print 'bye bye'
            GPIO.output(pin_id,True)
            GPIO.cleanup()
            request.close()
            sock.close()
            break
        else:
            if ':' not in data:
                continue

            #name = data.split(':')[0]
            #value = data.split(':')[1]

            #if value!='on' or value!='off':
            #    continue
            
            if ':on' in data:
                GPIO.output(pin_id,False)
            else:
                GPIO.output(pin_id,True)

            print data



if __name__ == '__main__':
    main()








